package com.example.week11;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;

import java.util.List;

public class RandomNumActivity extends AppCompatActivity {

    RandomNumService binderService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random_num);

        findViewById(R.id.randomNumBtn).setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                List number = binderService.getRandomNumber();
                String str="";
                for (int i=0;i<number.size();i++){
                    str+=(number.get(i).toString()+" ");
                }
                TextView randomNumTxt = (TextView) findViewById(R.id.randomNumTxt);
                randomNumTxt.setText(str);
            }
        });
    }
    protected void onStart(){
        super.onStart();
        Intent intent = new Intent(this,RandomNumService.class);
        bindService(intent,conn,BIND_AUTO_CREATE);
    }

    protected void onStop(){
        super.onStop();
        unbindService(conn);
    }

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binderService=((RandomNumService.MyBinder)service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

}